/*
数据结构 解题报告：
http://git.oschina.net/et0662/sznoi-Data_Structure
*/
#include<stdio.h>

typedef struct _NODE
{
	int data;
	struct _NODE * next;
}Node, *ptrNode, Stack, *ptrStack;

int isEmpty(ptrStack s)
{
	if(s->next)
	{
		return 0;
	}
	else
	{
		return 1;
	}
}

void push(ptrStack s, int n)
{
	ptrNode p;
	p=new Node;
	p->data=n;
	if(s->next)
	{
		p->next=s->next;
		s->next=p;
	}
	else
	{
		p->next=NULL;
		s->next=p;
	}
}

void pop(ptrStack s)
{
	ptrNode p;
	if(s->next)
	{
		p=s->next;
		s->next=p->next;
		delete p;
	}
}

void a(ptrStack s)
{
	if(s->next)
	{
		printf("%d\n",s->next->data);
	}
	else
	{
		printf("E\n");
	}
}

ptrStack initStack()
{
	ptrNode p;
	p=new Node;
	p->next=NULL;
	return p;
}

int main(void)
{
	int n,t;
	char c;
	ptrStack s;
	s=initStack();
	scanf("%d",&n);
	while(n--)
	{
		while( scanf("%c",&c), !(c=='P' || c=='A' || c=='O'));
		switch(c)
		{
			case 'P':
				scanf("%d",&t);
				push(s,t);
				break;
			case 'A':
				a(s);
				break;
			case 'O':
				pop(s);
				break;
		}
	}
	return 0;
}
